/* eslint-disable no-undef */
const util = {
    checkPermission: (role, permission) => {
        let perms = role.perms;
        let status = false;
        if (perms instanceof Array) {
            perms.forEach((perm) => {
                if (perm.name === permission) {
                    status = true;
                }
            });
        }
        return status;
    },
    checkedUserPermission: (user, key) => {
        let b = false;
        let roles = user.roles;
        if (roles instanceof Array) {
            roles.forEach((role) => {
                if (b === false) {
                    b = util.checkPermission(role, key);
                }
            });
        }
        return b;
    },
    getTrData (e) {
        let tr = $(e.srcElement).parents('tr');
        return {
            id: tr.attr('data-id'),
            name: tr.attr('data-name')
        };
    }
};

const install = function (Vue, options) {
    if (install.installed) return;
    Vue.prototype.$util = util;
};

if (typeof window !== 'undefined' && window.Vue) {
    install(window.Vue);
}

module.exports = {
    version: '1.0.0',
    install,
    util: util
};
